// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  email    String  @unique
  password String
  name     String?

  lists        UserList[] @relation("ListAccess")
  createdLists List[]     @relation("ListAuthor")

  listItems   ListItem[]   @relation("ListItemAuthor")
  ratingMarks RatingMark[] @relation("RatingMarksAuthor")

  @@map("user")
}

model List {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  title String

  accessUsers UserList[] @relation("ListAccess")

  authorId String @map("author_id")
  author   User   @relation("ListAuthor", fields: [authorId], references: [id], onDelete: Cascade)

  items ListItem[] @relation("ListItems")

  @@map("list")
}

model UserList {
  userId String @map("user_id")
  listId String @map("list_id")
  user   User   @relation("ListAccess", fields: [userId], references: [id], onDelete: Cascade)
  list   List   @relation("ListAccess", fields: [listId], references: [id], onDelete: Cascade)

  @@id([userId, listId])
  @@map("user_list")
}

model ListItem {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  title  String
  rating Float

  authorId String @map("author_id")
  author   User   @relation("ListItemAuthor", fields: [authorId], references: [id], onDelete: Cascade)

  listId String @map("list_id")
  list   List   @relation("ListItems", fields: [listId], references: [id], onDelete: Cascade)

  ratingMarks RatingMark[] @relation("RatingMarks")

  @@map("list_item")
}

model RatingMark {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  value Int

  authorId String @map("author_id")
  author   User   @relation("RatingMarksAuthor", fields: [authorId], references: [id], onDelete: Cascade)

  listItemId String   @map("list_item_id")
  listItem   ListItem @relation("RatingMarks", fields: [listItemId], references: [id], onDelete: Cascade)

  @@map("rating_mark")
}
